import { type FieldSchema, type MenuSchema, type ViewSchema } from 'aoe';

interface App {
    id: string;
}

const app: App = {
    id: 'aoe',
};

interface Context {
    app: App;
    req: Request;
    /**
     * 每次请求的中间数据
     */
    aoe: Record<string, unknown>;
    response?: Response;
}

type Middleware = (ctx: Context, next?: () => void | Promise<void>) => void | Promise<void>;
type FnAction = (ctx: Context) => void | Promise<void>;

const menu = {
    data: {
        component: 'menu',
        schema: {
            items: [
                {
                    id: '0',
                    label: 'Home',
                },
                {
                    id: 'A',
                    label: 'AAAAAAA',
                    items: [
                        {
                            id: 'A-1',
                            label: 'A-1',
                            url: '/menu/A-1',
                        },
                        {
                            id: 'A-2',
                            label: 'A-2',
                            url: '/menu/A-2',
                        },
                    ],
                },
                {
                    id: 'B',
                    label: 'BBBBBBB',
                    url: '/menu/B',
                },
            ],
        } as MenuSchema,
    } as ViewSchema,
};

const tree = {
    data: {
        component: 'tree',
        schema: {
            modelValue: [
                {
                    id: 'Home',
                    label: 'Home',
                },
                {
                    id: 'Article',
                    label: '文章',
                    children: [
                        {
                            id: '开发区',
                            label: 'A-1',
                            children: [
                                {
                                    id: 'A-1-1',
                                    label: 'A-1-1',
                                },
                                {
                                    id: 'A-1-2',
                                    label: 'A-1-2',
                                },
                            ],
                        },
                        {
                            id: 'A-2',
                            label: '尧都区',
                            open: true,
                        },
                    ],
                },
                {
                    id: 'B',
                    label: '论坛',
                    url: false,
                },
            ],
            fields: [
                {
                    id: 'label',
                    name: 'label',
                    widget: 'input',
                },
                {
                    id: 'open',
                    name: 'open',
                    widget: 'switch',
                },
            ] as FieldSchema[],
        },
    } as ViewSchema,
};

const login = {
    data: {
        component: 'login',
    } as ViewSchema,
};

const main = {
    data: {
        component: 'main',
    } as ViewSchema,
};

const pathinfo_mid: Middleware = async (ctx, next) => {
    const url = new URL(ctx.req.url);
    ctx.aoe.pathinfo = url.pathname;
    await next?.();
};

const core_mid: Middleware = async (ctx: Context, next) => {
    switch (ctx.aoe.pathinfo) {
        case '/menu':
            ctx.response = new Response(JSON.stringify(menu));
            break;
        case '/tree':
            ctx.response = new Response(JSON.stringify(tree));
            break;
        case '/login':
            ctx.response = new Response(JSON.stringify(login));
            break;
        case '/main':
            ctx.response = new Response(JSON.stringify(main));
            break;
    }

    await next?.();
};

const cors_mid: Middleware = async (ctx: Context, next) => {
    await next?.();
    ctx.response?.headers.set('Access-Control-Allow-Origin', 'http://localhost:5173');
    ctx.response?.headers.set('Access-Control-Allow-Methods', '*');
    ctx.response?.headers.set('Access-Control-Allow-Credentials', 'true');
};

const middlewares: Middleware[] = [pathinfo_mid, cors_mid, core_mid];

const compose = (middlewares: Middleware[]): FnAction => {
    const dispatch = (ctx: Context, i: number) => {
        if (middlewares.length === i) return;
        middlewares[i](ctx, () => dispatch(ctx, i + 1));
    };
    return (ctx: Context) => {
        middlewares[0](ctx, () => dispatch(ctx, 1));
    };
};

Bun.serve({
    fetch: async (req) => {
        const ctx: Context = {
            app,
            req,
            aoe: {},
            response: undefined,
        };
        const fn = compose(middlewares);
        await fn(ctx);
        return ctx.response ?? new Response('404');
    },
});
console.log('服务器已启动');
